const app = getApp()
Page({
	data: {
		resourceDomain: app.resourceDomain,
		num: 0,
		page: 1,
		// 健康币请求type id
		typeListId: null,
		// 类型
		type: null,
		// 商品列表
		goodsList: null,
		sou_list:[],
    isBottom: false
	},
	onLoad: function(options) {
		// console.log(options)
		wx.setNavigationBarTitle({
			title: options.title,
		})
		//
		var that = this
		// 类别
		var type = options.type
		//判断是健康币还是助力
		if (type == 'exchange') {
			// 健康币 请求横条
			if(options.sou_suo=='yes'){
				app.request(app.apiDomain + "index.php?c=mall&a=exchangegoodslist",{
					match_con:options.title
				},function(res) {
					// console.log(res.data)
					that.setData({
						// 商品列表
						goodsList: res.data.data,
						good_true: res.data.data == false ? 'good_off':'',
						// 类别
						type: type,
						sou_suo_on:'on',
						sou_suo_tiao:options.title,
					})
				})
				wx.setNavigationBarTitle({
					title:'搜索结果',
				})
			}else{
				app.request(app.apiDomain + 'index.php?c=mall&a=goodstype', {
					f_id: options.f_id
				}, res => {
					var typeList = res.data.data
					// 请求列表
					app.request(app.apiDomain + 'index.php?c=mall&a=exchangegoodslist', {
						page: 1,
						size: 20,
						f_id: typeList.length == 0 ? options.f_id : typeList[0].f_id
					}, res => {
						console.log(res)
						that.setData({
							// 导航列表
							typeList: typeList,
							// 商品列表
							goodsList: res.data.data,
							// 类别
							type: type,
							typeListId: typeList.length == 0 ? options.f_id : typeList[0].f_id,
						})
					})
				})
			}
		} else if (type == 'assist') {
			console.log(type != 'assist')
			// 助力  没有横条
      wx.showLoading({
        title: '加载中',
      })
      app.request(app.apiDomain + '/index.php?c=bargain&a=index&page=1&size=50', {}, res => {
				console.log('砍价',res)
        wx.hideLoading()
				that.setData({
					typeList: [],
					// 商品列表
					goodsList: res.data.data,
					// 类别
					type: type
				})
			})
      app.request(app.apiDomain + '/index.php?c=bargain&a=lastestorderlist', {}, res => {
        console.log('下单成功列表', res)
        that.setData({
          bannnerlist: res.data.data
        })
      })
		}
		//搜索记录
		if(wx.getStorageSync('sou_list')){
			that.setData({
				sou_list:wx.getStorageSync('sou_list')
			});
			console.log('把缓存放都data来',that.data.sou_list)
		}
		
		
	},
	clickType(e) {
		var that = this
		console.log(e)
		// console.log(e.target)
		app.request(app.apiDomain + 'index.php?c=mall&a=exchangegoodslist', {
			page: 1,
			size: 20,
			f_id: e.target.id
		}, res => {
			// console.log(res)
			that.setData({
				// 商品列表
				goodsList: res.data.data,
				num: e.target.dataset.id,
				typeListId: e.target.id
			})
			// 回到顶部
			if (wx.pageScrollTo) {
				wx.pageScrollTo({
					scrollTop: 0
				})
			} else {
				wx.showModal({
					title: '提示',
					content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
				})
			}
		})
	},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	scroll_get: function() {
    console.log(111)
		//     console.log(this.data.page)
		//     console.log(this.data.typeListId)
		//     console.log(this.data.goodsList)
		var that = this
		// 如果是助力 不用下拉加载
		if (this.data.type == 'exchange') {
			app.request(app.apiDomain + 'index.php?c=mall&a=exchangegoodslist', {
				page: this.data.page + 1,
				size: 20,
				f_id: this.data.typeListId,
			}, res => {
				console.log(res)
				if (res.data.data.length == 0) {
					wx.showToast({
						title: '没有更多了~',
						icon: 'none',
						duration: 2000
					})
					return false
				}
				that.setData({
					page: this.data.page + 1,
					// 商品列表
					goodsList: this.data.goodsList.concat(res.data.data)
				})
			})
    } else if (this.data.type == 'assist') {
      var page = this.data.page + 1
      app.request(app.apiDomain + '/index.php?c=bargain&a=index&page=' + page +'&size=50', {}, res => {
        console.log(res)
        if (res.data.data.length == 0) {
          wx.showToast({
            title: '没有更多了~',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            // page: this.data.page + 1,
            isBottom: true
          })
          return false
        } else if (res.data.data.length < 50) {
          wx.showToast({
            title: '没有更多了~',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            page: this.data.page + 1,
            // 商品列表
            goodsList: this.data.goodsList.concat(res.data.data),
            isBottom: true
          })
          return false
        }
        that.setData({
          page: this.data.page + 1,
          // 商品列表
          goodsList: this.data.goodsList.concat(res.data.data),
          isBottom: false
        })
      })
    }
	},
	sou_click:function(){
		var thas=this;
		this.setData({
			sou_suo:'on'
		})
		setTimeout(function(e=thas){
			e.setData({
				focus:true
			})
		},300)
	},
	sou_off:function(){
		this.setData({
			sou_suo:''
		})
	},
	sou_li_click:function(e){
		// console.log(e.currentTarget.dataset.words)
		var that=this
		app.request(app.apiDomain + "index.php?c=mall&a=exchangegoodslist",{
			match_con:e.currentTarget.dataset.words
		},function(res) {
			that.setData({
				// 商品列表
				goodsList: res.data.data,
				sou_suo:''
			})
		})
	},
	sou_suo_get:function(e){
		var that=this
		// console.log(e.detail.value)
		var name = e.detail.value;
		var detail = this.data.sou_list;
		detail.unshift(name)
		this.setData({
			sou_list:detail
		});
		wx.setStorage({
			key: "sou_list",
			data: detail ,
		})
		app.request(app.apiDomain + "index.php?c=mall&a=exchangegoodslist",{
			match_con:e.detail.value
		},function(res) {
			that.setData({
				// 商品列表
				goodsList: res.data.data,
				good_true: res.data.data == false ? 'good_off':'',
				sou_suo:''
			})
		})
	}
})
